/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System.Collections.Generic;

namespace OpenServers.Net.Common.FileSystem.Virtual {
    /// <summary>
    /// A virtual directory which exists only in memory. The subdirectories are held in a list.
    /// </summary>
    public class VirtualDirectory : VirtualFileSystemObject, IDirectory {
        private readonly IList<IFileSystemObject> _items = new List<IFileSystemObject>();

        /// <summary>
        /// constructor.
        /// </summary>
        public VirtualDirectory(IDirectory parent, string name)
            : base(parent, name) {
        }

        /// <summary>
        /// Gets the subitems (files and directories).
        /// </summary>
        public virtual IList<IFileSystemObject> GetItems() {
            return _items;
        }

        /// <summary>
        /// The list of directories only.
        /// </summary>
        public virtual IList<IDirectory> GetDirectories() {
            IList<IDirectory> results = new List<IDirectory>();
            foreach (IFileSystemObject item in _items) {
                if (item is IDirectory) {
                    results.Add((IDirectory)item);
                }
            }
            return results;
        }

        /// <summary>
        /// Creates the directory.
        /// </summary>
        public virtual void Create() {
            if (Parent != null && Parent is VirtualFileSystemObject) {
                VirtualFileSystemObject vfso = (VirtualFileSystemObject)Parent;
                vfso.Exists = true;
            }
            this.Exists = true;
        }

        /// <summary>
        /// creates a file and returns it.
        /// </summary>
        public virtual IFile CreateFile(string name) {
            return null;
        }

        /// <summary>
        /// Creates a sub directory.
        /// </summary>
        public virtual IDirectory CreateDirectory(string name) {
            return null;
        }

        /// <summary>
        /// Gets an object from the directory.
        /// </summary>
        /// <param name="path">The path is seperated by '/'</param>
        public IFileSystemObject GetObject(string path) {
            return FileSystemUtils.GetObject(this, path);
        }
    }
}
